<template>
    <el-dialog width="720px" title="修改密码" :close-on-click-modal='false' :visible.sync="dialogOpen" @close="close('form')" append-to-body class="newEditContainer">
        <el-form label-width="180px" class="newEdit" :model="form" :rules="formRules" ref="form" >
                  <p style="font-size: 16px;width: 100%;text-align: center;"><i class="el-icon-info" style="color: #3ca0ec;margin-right: 20px;margin-bottom: 20px"></i>你好{{userName}}，初次登陆，为了提高账号安全性，请修改密码后再登陆</p>
               <el-form-item label="你的登录密码" prop="passWord"  class="el-form-margin">
                    <el-input type="password" v-model="form.passWord" clearable  placeholder="请输入登录密码（6-20位数字或字符）"  style="width:400px" ></el-input>
                </el-form-item>
                <el-form-item label="新登陆密码" prop="newPassWord" class="el-form-margin">
                    <el-input type="password" v-model="form.newPassWord" clearable placeholder="请输入新密码（6-20位数字或字符）"  style="width:400px" ></el-input>
                </el-form-item>
                <el-form-item label="再次输入密码" prop="confirmPassWord" class="el-form-margin">
                    <el-input type="password" v-model="form.confirmPassWord" clearable placeholder="请再一次输入密码（6-20位数字或字符）" @keyup.enter.native="submit('form')" style="width:400px" ></el-input>
                </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submit('form')">提交</el-button>
            <el-button @click="close('form')">取消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { changePwd } from '@/api/user'

export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        if (value === this.form.passWord) {
          callback(new Error('新密码不能与旧密码相同'))
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.form.newPassWord) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      userName: '',
      form: {

      },
      formRules: {
        passWord: [
          { max: 20, min: 6, message: '密码长度为6-20位', trigger: 'blur' },
          { required: true, message: '请输入原密码', trigger: 'blur' }
        ],
        newPassWord: [
          { max: 20, min: 6, message: '密码长度为6-20位', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        confirmPassWord: [
          { max: 20, min: 6, message: '密码长度为6-20位', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      formParams: {}, // 提交接口参数
      loading: false,
      dialogOpen: false,
      nowTime: new Date()
    }
  },
  methods: {
    async open (val) {
      this.dialogOpen = true
      this.userName = val
    },
    // 关闭弹窗
    close (formName) {
      this.$refs[formName].resetFields()
      this.dialogOpen = false
    },
    // 提交
    async submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const form = Object.assign({}, this.form)
          form.saleName = this.userName
          changePwd(form).then(res => {
            this.$router.push({ path: '/index' })
            this.$router.go(0)
            this.$message.success('密码已修改，正在登录系统...')
          })
        } else {
          return false
        }
      })
    }

  },
  async created () {

  }

}
</script>

<style scoped>
</style>
